lobsterh5.main({
    data: {
        // loading: false, //状态标记
        // page: 1,
        // perPage: 5,
        // flag: true,
        weeks: [],

        // doctorlist: [],
        // todaySchedule: [],
        // orderSchedule: [],

        deptId: -1,
        // schedList: [],
        selectedId: '', //选择的日期
        selectdate: '',
        showdoctordata: [],

        // selectItemInfo: {}, //点击医生信息
        // currentbranch: {},//当前分院
    },
    //初始化页面
    pageload: function () {
        var self = this;
        self.data.deptId = lobsterh5.GetUrlParam("deptId");
        // self.data.currentbranch = JSON.parse(lobsterh5.GetAppStorage("selectbranch")) || {};
        self.initevent();
        self.initdata();
    },
    initdata() {
        var self = this;
        self.data.weeks = self.getweekday();
        self.data.selectedId = self.data.weeks[0].Day;
        self.data.selectdate = self.data.weeks[0].RegDate;
        $('#only').hide();
        $.showLoading();
        lobsterh5.GETAPI('tsregister.getAllSchedule', { deptId: self.data.deptId }).then(retdata => {
            console.log(retdata.list);
            //计算顶部日期是否有号
            self.data.weeks.forEach(day => {
                retdata.list.forEach(doc => {//循环医生
                    if (doc.ItemA) {//找到相同时间，则设置为当天有号
                        if (doc.ItemA.map(function (e) { return e.RegDate; }).indexOf(day.RegDate) > -1) {
                            day.DayHave = true;
                        }
                    }
                })
            })
            $("#only").show();
            $("#content").show();
            // $("#weui-loadmore").hide();
            self.inittemp("tpldayweek", "dayweek", self.data.weeks);
            self.daySchedule();
            // $.hideLoading();
        })
    },

    //初始化事件
    initevent: function () {
        var self = this;
        //点击顶部日期
        $(document).on("click", ".dayofweek", function () {
            $(".day_selected").removeClass("day_selected");
            $(this).addClass('day_selected').siblings('div').removeClass('day_selected');
            var data = $(this).data("info");
            self.seleDate(data);
        })
        // $(document).on("click", "#all", function () {
        //     $("#all").addClass("day_selected");
        //     $(".dayofweek").removeClass("day_selected");
        // })
        //点击只看有号
        $(document).on("click", "#isdefault", function () {
            var checked = $("#isdefault").is(":checked");
            var showdata = [];
            if (checked) {
                showdata = self.data.showdoctordata.filter(p => p.IsHave);
            } else {
                showdata = self.data.showdoctordata;
            }
            self.inittemp("tpldoctor", "docList", showdata);
        });
        //点击剩余号源
        $("#docList").on("click", ".toregister", function (obj) {
            obj.stopPropagation();

            var info = $(this).data("info");
            // self.data.selectItemInfo = info;
            console.log(info);
            if (info.HaveCount <= 0) {
                $.toast("已无号源", 'text');
            } else {
                self.inittemp("tplrange", "half", []); //清空
                $("#half").popup();
                self.getdoctorrange(info, function () {
                    //点击时段
                    $(document).on("click", ".otc_timeview", function () {
                        var info2 = $(this).data("info");
                        if (info2.HaveCount > 0 && info2.Status==0 && info2.IsExpire==0) {
                            localStorage.setItem("registerrecord", JSON.stringify(info2));
                            location.href = "sure.html?appid=" + lobsterh5.GetAppId()+ "&regtype=1";
                        } else {
                            $.toast("无法操作", 'text');
                        }
                    });
                });
            }
        });
        //点击医生列表
        $(document).on("click", ".todoctorinfo", function (obj) {
            obj.stopPropagation();
            var info = $(this).data("info");
            location.href = "../doctorinfo.html?appid=" + lobsterh5.GetAppId() + "&doctId=" + info.DoctId + "&deptId=" + info.DeptId;
        });

    },
    //选择日期
    seleDate(info) {
        this.data.selectedId = info.Day;
        this.data.selectdate = info.RegDate;
        this.daySchedule();
    },
    inittemp(tempid, _id, data) {
        var html = template(tempid, { list: data });
        $("#" + _id).html(html);
    },
    //显示排班时间段
    getdoctorrange(info, callback) {
        var self = this;
        var param = {
            schedtype: 0,
            doctId: info.DoctId,
            deptId: self.data.deptId,
        };
        $.showLoading();
        lobsterh5.GETAPI("tsregister.dayTimeRange", param).then(res => {
            console.log(res);
            var list = res.list;

            self.inittemp("tplrange", "half", list);
            // $(".weui-loadmore").hide();
            $.hideLoading();
            callback && callback();
        }).catch(res => {
            console.log(res);
            // $(".weui-loadmore").hide();
            $.hideLoading();
        })
    },
    //周是否有排班
    //今日排班
    getweekday() {
        var self = this;

        var list = [];
        var dateNow = moment().format('YYYY-MM-DD');

        list.push({
            DayHave: false,
            RegDate: '',
            Week: '全部',
            Day: -1
        });

        list.push({
            DayHave: false,
            RegDate: dateNow,
            Week: '今天',
            Day: self.daynum(dateNow)
        });

        for (var j = 1; j < 7; j++) {

            var time = moment().add(j, 'days').format('YYYY-MM-DD');
            var orderItem = {};
            orderItem.DayHave = false;
            orderItem.RegDate = time;
            orderItem.Week = self.dayweek(time);
            orderItem.Day = self.daynum(time);
            list.push(orderItem);
        }

        return list;
    },
    dayweek(time) {
        var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        time = new Date(time);
        return weekday[time.getDay()];
    },
    daynum(time) {
        time = new Date(time);
        return time.getDate();
    },
    //单日数据
    daySchedule() {
        var self = this;
        // $("#weui-loadmore").show();
        $.showLoading();
        $("#docList").html("");
        var data = {
            date: self.data.selectdate,
            deptId: self.data.deptId
        };
        lobsterh5.GETAPI("tsregister.daySchedule", data).then(retdata => {

            self.data.showdoctordata = retdata.list;

            // $("#weui-loadmore").hide();
            $.hideLoading();
            self.inittemp("tpldoctor", "docList", retdata.list);
        }).catch(res => {
            console.log(res);
            // $("#weui-loadmore").hide();
            $.hideLoading();
        })
    },

}, false);